package com.lolong.hic;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class SearchActivity extends Activity implements OnItemSelectedListener, OnItemClickListener, OnClickListener {
	
	private ImageButton backBtn;
	private Spinner typeSpnr;
	private ListView estabsLView;
	private Toast toast;
	private SQLiteConnector connector;
	
	private String categorySelected;
	private String[] foodTypesArray = { "Fast Food", "Casual Dining", "Beverages", "Desserts"};
	private String[] fashionTypesArray = { "Footwear", "Clothes", "Accessories", "Cosmetics"};
	private String[] electronicsTypesArray = { "Personal Gadgets", "Home"};
	private ArrayAdapter<String> spinnerAdapter, listViewAdapter;
	private ArrayList<String> establishmentsArrayList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.search);
		
		initComponents();
	}
	

	public void initComponents() {
		
		backBtn = (ImageButton) findViewById(R.id.back);
		backBtn.setOnClickListener(this);
		
		
		toast = new Toast(this);
		establishmentsArrayList = new ArrayList<String>();
		connector = new SQLiteConnector(this);
		
		typeSpnr = (Spinner) findViewById(R.id.typeSpinner);
		initSpinnerAdapter();
		typeSpnr.setAdapter(spinnerAdapter);
		typeSpnr.setOnItemSelectedListener(this);
		
		estabsLView = (ListView) findViewById(R.id.estabsListView);
		estabsLView.setOnItemClickListener(this);
	}


	public void initSpinnerAdapter() {
		
		Bundle extras = getIntent().getExtras();
		
		if (extras != null) {
		    categorySelected = extras.getString("Category");
		}
		
		if(categorySelected.equalsIgnoreCase("food")) {
			spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, foodTypesArray);
			
		} else if(categorySelected.equalsIgnoreCase("fashion")) {
			spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, fashionTypesArray);

		} else {
			spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, electronicsTypesArray);
		}
	}
	
	
	private void initListViewAdapater() {
		// TODO Auto-generated method stub
		
		listViewAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, establishmentsArrayList);
	}
	
	
	//Spinner
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		
		if(categorySelected.equalsIgnoreCase("food")) {
			
			handleFoodSpinner();
			
		} else if(categorySelected.equalsIgnoreCase("fashion")) {
			
			handleFashionSpinner();
			
		} else {
			
			handleElectronicSpinner();
			
		}
		
		if(establishmentsArrayList.isEmpty()) {
			
			toast.makeText(this, "No result found.", 2).show();
			establishmentsArrayList.clear();
			initListViewAdapater();
			estabsLView.setAdapter(listViewAdapter);
		} else {
			initListViewAdapater();
			estabsLView.setAdapter(listViewAdapter);
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
		Intent intent = new Intent(SearchActivity.this, InformationActivity.class);
		intent.putExtra("establishmentName", estabsLView.getItemAtPosition(arg2).toString());
		startActivity(intent);
		//toast.makeText(this, estabsLView.getItemAtPosition(arg2).toString(), 2).show();
	}
	
	
	public void handleFoodSpinner() {
		
		switch(typeSpnr.getSelectedItemPosition()) {
		
		case 0:
			//Populate array list with fastfood
			establishmentsArrayList.clear();
			establishmentsArrayList.addAll(connector.getEstablishmentNames("Fastfood"));
			break;
			
		case 1:
			//Populate array list with casual dining
			establishmentsArrayList.clear();
			establishmentsArrayList.addAll(connector.getEstablishmentNames("Casual Dining"));
			break;
			
		case 2:
			//Populate array list with beverages
			establishmentsArrayList.clear();
			establishmentsArrayList.addAll(connector.getEstablishmentNames("Beverages"));
			break;
			
		case 3:
			//Populate array list with desserts
			establishmentsArrayList.clear();
			establishmentsArrayList.addAll(connector.getEstablishmentNames("Desserts"));
			break;
		}
	}

	
	private void handleFashionSpinner() {
		// TODO Auto-generated method stub
		switch(typeSpnr.getSelectedItemPosition()) {
		
		case 0:
			//Populate array list with footwear
			establishmentsArrayList.clear();
			establishmentsArrayList.addAll(connector.getEstablishmentNames("Footwear"));
			break;
			
		case 1:
			//Populate array list with clothes
			establishmentsArrayList.clear();
			establishmentsArrayList.addAll(connector.getEstablishmentNames("Clothes"));
			break;
			
		case 2:
			//Populate array list with accessories
			establishmentsArrayList.clear();
			establishmentsArrayList.addAll(connector.getEstablishmentNames("Accessories"));
			break;
			
		case 3:
			//Populate array list with cosmetics
			establishmentsArrayList.clear();
			establishmentsArrayList.addAll(connector.getEstablishmentNames("Cosmetics"));
			break;
		}
	}
	
	private void handleElectronicSpinner() {
		// TODO Auto-generated method stub
		switch(typeSpnr.getSelectedItemPosition()) {
		
		case 0:
			//Populate array list with Personal Gadget
			establishmentsArrayList.clear();
			establishmentsArrayList.addAll(connector.getEstablishmentNames("Personal Gadgets"));
			break;
			
		case 1:
			//Populate array list with Home/Appliances
			establishmentsArrayList.clear();
			establishmentsArrayList.addAll(connector.getEstablishmentNames("Home"));
			break;
		}
	}


	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search, menu);
		return true;
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch (v.getId()) {
			
		case R.id.back:
			Intent intent = new Intent(SearchActivity.this, CategoryActivity.class);
			startActivity(intent);
		
		}
		
	}
}
